package ltd.dujiabao.eduvod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.commonutils.Result;
import ltd.dujiabao.commonutils.ResultCode;
import ltd.dujiabao.eduvod.service.VideoService;
import ltd.dujiabao.eduvod.utils.AliyunVodSDKUtils;
import ltd.dujiabao.eduvod.utils.ConstantPropertiesUtil;
import ltd.dujiabao.servicebase.exception.MyException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author : DuJiabao
 * @Project : online-course-project
 * @Time : 2021/4/23 10:26
 * @Desc :
 */
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {
  @Override
  public String uploadVideo(MultipartFile file) {
    String accessKeyId = ConstantPropertiesUtil.KEY_ID;
    String accessKeySecret = ConstantPropertiesUtil.KEY_SECRET;
    String fileName = file.getOriginalFilename();
    String title = fileName.substring(0, fileName.lastIndexOf("."));

    InputStream inputStream = null;
    try {
      inputStream = file.getInputStream();
    } catch (IOException e) {
      e.printStackTrace();
      log.error("无法获取文件流");
      throw new MyException(ResultCode.FAILURE, "无法获取文件流");
    }

    UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
    UploadVideoImpl uploader = new UploadVideoImpl();
    UploadStreamResponse response = uploader.uploadStream(request);
    log.info("RequestId=" + response.getRequestId());  //请求视频点播服务的请求ID

    String videoId = response.getVideoId();
    if (!response.isSuccess()) {
      /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */
      log.warn("ErrorCode=" + response.getCode());
      log.warn("ErrorMessage=" + response.getMessage());
    }
    if (StringUtils.isEmpty(videoId)) {
      log.error("videoId未知");
      throw new MyException(ResultCode.FAILURE, "videoId未知");
    }
    log.info("Have upload a video, title: " + title);
    log.info("videoId=" + response.getVideoId());
    return videoId;
  }

  @Override
  public boolean removeVideo(String videoIds) {
    String accessKeyId = ConstantPropertiesUtil.KEY_ID;
    String accessKeySecret = ConstantPropertiesUtil.KEY_SECRET;
    String regionId = ConstantPropertiesUtil.REGION_ID;
    DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret, regionId);
    DeleteVideoResponse response = new DeleteVideoResponse();
    try {
      response = AliyunVodSDKUtils.deleteVideo(client, videoIds);
    } catch (Exception e) {
      log.info("删除视频失败，ErrorMessage = " + e.getLocalizedMessage());
      throw new MyException(ResultCode.FAILURE, "删除视频失败");
    }
    log.info("RequestId = " + response.getRequestId());
    return true;
  }
}
